package com.example.wechatdaka.interceptor;

import com.example.wechatdaka.entity.User;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class AuthInterceptor extends HandlerInterceptorAdapter
{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        // 拦截处理代码
        System.out.println("拦截到了");
        String url=request.getRequestURI();
        String username= (String) request.getSession().getAttribute("username");
        if(username == null){
            response.sendRedirect(request.getContextPath()+"/");
            System.out.println("用户未登录");
            return false;
        }
        System.out.println("url:"+url);
        //返回true通过，返回false拦截
        return true;
    }
}